using System.Threading;
using ebcpatterns.concurrent;
using NUnit.Framework;

namespace ebcpatterns.tests.concurrent
{
    [TestFixture]
    public class Asynchronizer_with_Parameters_Tests
    {
        private EventWaitHandle waitHandle;
        private Asynchronizer<int> sut;

        [SetUp]
        public void Setup() {
            sut = new Asynchronizer<int>();
            waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
        }

        [Test]
        public void Result_event_is_raised_with_parameter() {
            var result = 0;
            sut.Result += x => {
                result = x;
                waitHandle.Set();
            };
            sut.Process(42);

            Assert.That(waitHandle.WaitOne(500), Is.True);
            Assert.That(result, Is.EqualTo(42));
        }

        [Test]
        public void Result_event_is_raised_on_different_thread() {
            var currentThread = Thread.CurrentThread;

            sut.Result += x => {
                Assert.That(Thread.CurrentThread, Is.Not.SameAs(currentThread));
                waitHandle.Set();
            };

            sut.Process(1);
            Assert.That(waitHandle.WaitOne(500), Is.True);
        }
    }
}